package reader;

import java.io.*;
import java.io.FileReader;

/**
 * @author feng
 * @Date 2021/12/26 21:14
 * 缓冲流实现文件拷贝
 * BufferedReader不要去操作二进制文件(视频、声音、doc文件等)，可能会导致文件损坏
 */
public class BufferCopy {
    public static void main(String[] args) {
        String source = "D:\\IO\\4.text";
        String dest = "D:\\IO\\A\\B\\4.text";
        BufferedWriter writer1 = null;
        BufferedReader reader = null;
        String s;
        try {
            FileReader fileReader = new FileReader(source);
            FileWriter writter = new FileWriter(dest);
            reader = new BufferedReader(fileReader);
            writer1 = new BufferedWriter(writter);
            while ((s = reader.readLine()) != null) {
                //读取一行
                writer1.write(s);
                //换行
                writer1.newLine();
            }
            System.out.println("拷贝完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流.底层关闭。！！！！！！！！！！！
            try {
                if (writer1 != null) {
                    writer1.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
